package com.atwulidun.flyweight10;

import java.util.HashMap;
import java.util.Map;

// 享元工厂
public class Library {
    // 维护一个图书(借阅时有该图书，直接借出；若没有，添加该图书进列表再借出)
    private static Map<String, Book> booksPool = new HashMap<>();
    // 把Library设定为单例模式
    private static final Library instance = new Library();

    public static Library getInstance() {
        return instance;
    }

    public static Map<String, Book> getBooksPool() {
        return booksPool;
    }

    // 借阅图书的方法
    public static Book borrow(String name) {
        Book book = null;
        if (booksPool.containsKey(name)) {
            book = booksPool.get(name);
        } else {
            book = new ConcretBook(name);
            booksPool.put(name, book);
        }
        return book;
    }

    // 返回图书列表中图书数量的方法
    public static int getbookPoolSize() {
        return booksPool.size();
    }
}
